package com.polaris.live.utils.viewscope

import android.view.View
import kotlinx.coroutines.CompletionHandler
import kotlinx.coroutines.Job

/**
 * ViewStateListener view移除界面和协程回调监听
 * @author Created by 天晴 on 2024/1/10 8:58
 * @since 1.0.0
 **/
class ViewStateListener(
    val view: View,
    val job: Job
) : View.OnAttachStateChangeListener, CompletionHandler {
    override fun onViewAttachedToWindow(v: View) {
    }

    override fun onViewDetachedFromWindow(v: View) {
        view.removeOnAttachStateChangeListener(this)
        job.cancel()
    }

    override fun invoke(cause: Throwable?) {
        view.removeOnAttachStateChangeListener(this)
        job.cancel()
    }


}